package com.dhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dhx.common.CustomException;
import com.dhx.domain.Category;
import com.dhx.dao.CategoryDao;
import com.dhx.domain.Dish;
import com.dhx.domain.Setmeal;
import com.dhx.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhx.service.IDishService;
import com.dhx.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author 邓鹄翔
 * @since 2022-03-08
 */

/**
 * 根据id删除
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements ICategoryService {
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetmealService setmealService;

    @Override
    public void remove(Long id) {
        //根据菜品条件删除
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(dishWrapper);
        if(count>0){
            throw new CustomException("当前分类下关联了菜品，不能删除");//已经关联菜品，抛出一个业务异常
        }
        //根据菜品条件删除
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(setmealWrapper);
        if(count1>0){
            throw new CustomException("当前分类下关联了套餐，不能删除");//已经关联菜品，抛出一个业务异常
        }
        //正常删除分类
        super.removeById(id);
    }
}
